package com.bainuo.doctor.common.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.bainuo.doctor.common.R;


/**
 * Created by luwei on 2017/10/13.
 */

public class ClearEditText extends CustomEditText implements TextWatcher {
    /**
     * 删除按钮
     */
    private Drawable mDelDrawable;
    private boolean isClear = true;
    private boolean isPwdType = true;
    private boolean isShowClearBtn = true; //是否显示一键清除按钮

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    public void setClearBtn(boolean isShow){
        isShowClearBtn = isShow;
        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
    }
    private void initView() {
        mDelDrawable = getResources().getDrawable(R.drawable.ease_search_clear_normal);
        addTextChangedListener(this);
    }

    public void setRightDrawable(int res, boolean isClear) {
        mDelDrawable = getResources().getDrawable(res);
        this.isClear = isClear;
        postInvalidate();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (editable.length() > 0 && isShowClearBtn) {
            setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, mDelDrawable, null);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (getCompoundDrawables()[2] != null) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                //getTotalPaddingRight()图标左边缘至控件右边缘的距离
                //getWidth() - getTotalPaddingRight()表示从最左边到图标左边缘的位置
                //getWidth() - getPaddingRight()表示最左边到图标右边缘的位置
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));

                if (touchable && isClear) {
                    this.setText("");
                }
            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable && !isClear) {
                    setSelection(getText().length());
                    if (isPwdType) {
                        setRightDrawable(R.mipmap.icon_drzy, false);
                        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        isPwdType = !isPwdType;
                    } else {
                        setRightDrawable(R.mipmap.icon_dryc, false);
                        setTransformationMethod(PasswordTransformationMethod.getInstance());
                        isPwdType = !isPwdType;
                    }
                }
            }

        }
        return super.onTouchEvent(event);
    }
}